import { Toast } from 'antd-mobile'
import Item from 'antd-mobile/es/components/dropdown/item'
import { filterPrice } from '../../filters'
import {reqcartlist,reqcartupdate,reqcartdel} from '../../request/api'
import { getuserinfo } from './login'
let isreq=true
//初始状态
const initState={
        cartlist:[],//购物车列表 
        all:false,//全选
}

//TYPES
const TYPES={
        CAHNGE_CARTLIST:'CAHNGE_CARTLIST',
        CHANGE_CART_ALL:'CHANGE_CART_ALL',
}

//reducer
const reducer =(state=initState,action)=>{
        switch(action.type){
                case TYPES.CAHNGE_CARTLIST:
                        return{
                                ...state,
                                cartlist:action.cartlist
                        }
                case TYPES.CHANGE_CART_ALL:
                        return{
                                ...state,
                                all:action.all
                        }
                default:
                        return state
        }
}

//creators 做逻辑 方法  请求
export const actions={
        changecartlsit:cartlist=>({type:TYPES.CAHNGE_CARTLIST,cartlist}),
        //请求购物车列表 init
        requestcartList:()=>(dispatch,getState)=>{
                let uid=getuserinfo(getState()).uid
                reqcartlist({uid}).then(res=>{
                        if(res.data.code==200){
                                let oldlist=getcartList(getState())
                                let newlist =res.data.list[1].content?res.data.list[1].content:[]

                                newlist.forEach(item=>{
                                        let obj = oldlist.find(i=>i.id==item.id);//返回true或false
                                        item.checked=obj?obj.checked:false
                                })
                                dispatch(actions.changecartlsit(newlist))

                                // 修改全选  删除所有全选改变
                                dispatch(actions.changeAllChecked(newlist.length>0?newlist.every(item=>item=item.checked):false))

                                //下一次请求
                                isreq=true
                        }
                })
        },
        //加加
        sum:(id)=>(dispatch,getState)=>{
                reqcartupdate({id,type:2}).then(res=>{
                        if(res.data.code==200){
                                dispatch(actions.requestcartList())
                        }
                })
        },
        // 减减
        sub:(id,num)=>(dispatch,getState)=>{
                if(num<=1){
                        Toast.show({
                                content:'宝贝不能再少啦'
                        })
                        return
                }
                // 节流  
               if(isreq){
                       isreq=false
                       reqcartupdate({id,type:1}).then(res=>{
                        if(res.data.code==200){
                                dispatch(actions.requestcartList())
                        }
                })
               }  
        },
        // 删除
        cartdel:(id)=>(dispatch,getState)=>{
                reqcartdel({id}).then(res=>{
                        if(res.data.code==200){
                                dispatch(actions.requestcartList())
                        }
                })
        },
        // 修改全选
        changeAllChecked:(all)=>({type:TYPES.CHANGE_CART_ALL,all}),
        // 单选
        changeOne:(e,index)=>(dispatch,getState)=>{
                let list =getcartList(getState());
                list[index].checked=e.target.checked;
                // 单选变了，全选变
                dispatch(actions.changeAllChecked(list.every(item=>item=item.checked)))
                //放
                dispatch(actions.changecartlsit([...list]))
        },
        // 全选
        changeAll:(e)=>(dispatch,getState)=>{
                let newStatus=e.target.checked;
                // 修改全选
                dispatch(actions.changeAllChecked(newStatus));

                // 点击全选 修改单选各项
                let list =getcartList(getState())
                list.forEach(item=>item.checked=newStatus)
                dispatch(actions.changecartlsit([...list]))
        }
}

// 导出数据给组件
export const getcartList=state=>state.shop.cartlist
export const getAllchecked=state=>state.shop.all
// 总价
export const getTotails=state=>{
        let list =getcartList(state)
        let sum =0;
        list.forEach(item=>{
                if(item.checked){
                        sum+=item.price*item.num
                }
                
        })
        return filterPrice(sum)
}
// 导出reducer
export default reducer